<template>
	<view class="login-bg" :style="{background:'url(/static/tabbar/02.png) no-repeat',backgroundSize:'100%'}">
		<!-- 		<u-navbar placeholder autoBack bgColor="transparent"></u-navbar> -->
		<!-- <view class="usertop flex align-center justify-center">
			<u-image src="/static/tabbar/index_s.png" width="168rpx" height="188rpx"></u-image>
		</view> -->

		<view class="flex align-center justify-center" style="margin-top: 950rpx;" >
			<image class="logo-image"src="/static/tabbar/03.png" mode=""></image>
		</view>
		<view class="flex align-center justify-center" style="margin-top: 30rpx;">
			<!-- <button class="loginBtn" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">微信一键登录</button> -->
			<button class="loginBtn"  @click="getOpenid">微信一键登录</button>
		</view>

		<view class="flex align-center justify-center" style="margin-top: 30rpx;">
			<u-image :src="isagree?'/static/select.png':'/static/noselect.png'" width="20" height="20"
				@click="isagree=!isagree"></u-image>
			<view class="ml-1 font-24" style="color:#BDBDBD;">
				我已阅读并同意<text style="color: #008F3D;"
					@click="$u.route('pages/login/agreement',{type:'privacy'})">《隐私政策》</text>和<text
					style="color: #008F3D;" @click="$u.route('pages/login/agreement',{type:'user'})">《用户协议》</text>
			</view>
		</view>

	</view>
</template>

<script>
	import {
		base
	} from '@/request/env.js'
	export default {
		data() {
			return {
				account: '',
				password: '',
				code: '',
				isloginPWD: true,
				tips: '',
				openId: '',
				isagree: false,
				bgUrl: '',
				invite_code: '',
				data: {}
			}
		},
		computed: {
			isable() {
				if (this.isloginPWD) {
					if (!uni.$u.test.isEmpty(this.account) && !uni.$u.test.isEmpty(this.password) && this.isagree) {
						return true
					} else {
						return false
					}
				} else {
					if (!uni.$u.test.isEmpty(this.account) && !uni.$u.test.isEmpty(this.code) && this.isagree) {
						return true
					} else {
						return false
					}
				}

			}
		},
		onLoad(e) {
			this.invite_code = e.code
			// this.bgUrl = base+'/image/login.png'
			let invite_code = uni.getStorageSync('invite_code')
			if (invite_code) {
				this.invite_code = invite_code
			}
		},
		onShow() {
			// this.getOpenid()
		},
		methods: {
			getOpenid() {
				if (!this.isagree) {
					return uni.showToast({
						title: '请阅读并勾选协议',
						icon: 'none'
					});
				}
				// 是否已经绑定
				let that = this
				uni.login({
					provider: 'weixin',
					success: (e) => {
						that.$api.getOpenidByCode({
							code: e.code
						}).then(res => {
							console.log(res)
							if (res.code === 1) {
								if (res.data.openid) {
									that.openId = res.data.openid
								} else {
									this.$store.commit('user/setUserInfo', res.data.userinfo)
									this.$store.commit('user/setToken', res.data.userinfo.token)
									uni.setStorageSync('userinfo', res.data.userinfo)
									uni.showToast({
										title: res.msg,
										icon: 'none'
									});
									setTimeout(() => {
										uni.switchTab({
											url: '/pages/index/index'
										})
									}, 1000)
								}
							}
						})
					}
				})
			},
			//获取手机号
			getPhoneNumber(e) {
				if (!this.isagree) {
					return uni.showToast({
						title: '请阅读并勾选协议',
						icon: 'none'
					});
				}
				if (!e.detail.iv) {
					uni.showToast({
						title: '获取手机号失败',
						icon: 'none'
					})
					return;
				}
				var that = this;
				//验证code值是否过期
				uni.checkSession({
					success(val) {
						if (val.errMsg == 'checkSession:ok') {
							var obj = {
								code: e.detail.code,
								iv: e.detail.iv,
								encryptedData: e.detail.encryptedData,
								openid: that.openId
							}
							console.log(obj)
							that.decryptPhone(obj);
						} else {
							uni.login({
								provider: 'weixin',
								success(res) {
									let code = res.code;
									var obj = {
										code,
										iv: e.detail.iv,
										encryptedData: e.detail.encryptedData
									}
									that.decryptPhone(obj);
								}
							})
						}
					}
				})

			},
			//解密手机号
			decryptPhone(obj) {
				this.$api.decryptPhone({
						code: obj.code,
						encryptedData: obj.encryptedData,
						iv: obj.iv,
						openid: obj.openid,
						invite_code: this.invite_code,
					}).then(res => {
						if (res.code == 1) {
							this.$store.commit('user/setUserInfo', res.data.userinfo)
							this.$store.commit('user/setToken', res.data.userinfo.token)
							uni.setStorageSync('userinfo', res.data.userinfo)
							uni.showToast({
								title: res.msg,
								icon: 'none'
							});
							setTimeout(() => {
								uni.switchTab({
									url: '/pages/index/index'
								})
							}, 1000)
						} else {
							if (this.source == 2) {
								that = this
								uni.login({
									success(e) {
										that.code = e.code
									}
								})
							}
							uni.showToast({
								title: '获取手机号失败，请重试',
								icon: 'none'
							})
						}
					})
					.catch(res => {
						uni.showToast({
							title: '获取手机号失败，请重试',
							icon: 'none'
						})
					})
			},
		}
	}
</script>

<style>
	.login-bg{
		width: 100%;
		height: 100vh;
		overflow: hidden;
	}
	.logo-image {
		width: 238rpx;
		height: 88rpx;
	}

	.usertop {
		background-size: 100% 272rpx;
		height: 272rpx;
		margin-top: 150rpx;
	}

	.loginBtn {
		width: 690rpx;
		height: 90rpx;
		background: #008F3D;
		border-radius: 14rpx;
		font-weight: normal;
		font-size: 30rpx;
		color: #FFFFFF;
		margin-top: 120rpx;
		letter-spacing: 5rpx;
		line-height: 90rpx;
	}
</style>